package com.atguigu03.threadsafe.runnablesafe.runnablesage;
/*
* 同步方法-Runnable接口
* */
public class WindowsTest1 {
    public static void main(String[] args) {
        SaleTickets2 s1=new SaleTickets2();
        Thread t4=new Thread(s1);
        Thread t5=new Thread(s1);
        Thread t6=new Thread(s1);

        t4.setName("线程1");
        t5.setName("线程2");
        t6.setName("线程3");

        t4.start();
        t5.start();
        t6.start();
    }


}

class SaleTickets2 implements Runnable{
    int ticket=100;
    Object o=new Object();
    boolean falg=true;
    @Override
    public void run() {
        while(falg){
                show();
        }
    }

    public synchronized void show(){
        if(ticket>0){
            try {
                Thread.sleep(10);
            }catch (InterruptedException e){
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+"售票，票号为："+ticket);
            ticket--;
        }else{
            falg=false;
        }
    }
}